import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'portfolioTrialAddM',
  state: {},
  effects: {
    // 交易所债券新增
    *bourseBondTradeSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.bourseBondTradeSave, payload);
      callback(response);
    },
    // 银行间债券新增
    *bankBondTradeSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.bankBondTradeSave, payload);
      callback(response);
    },
    // 上交所固守新增
    *stockBondTradeSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.stockBondTradeSave, payload);
      callback(response);
    },
    // 大宗现券新增
    *daZongBondTradeSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.daZongBondTradeSave, payload);
      callback(response);
    },
    // 基金新增
    *fundTradeSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.fundTradeSave, payload);
      callback(response);
    },
    // 交易所质押式回购新增
    *exchangePledgSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.exchangePledgSave, payload);
      callback(response);
    },
    // 交易所协议式回购新增
    *exchangeAgreementSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.exchangeAgreementSave, payload);
      if (response && response.success) {
        message.success('保存成功!');
      } else {
        message.error(response.errorMsg || '保存失败!');
      }
      return response;
    },
    // 银行间质押式回购新增
    *interBankSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.interBankSave, payload);
      callback(response);
    },
    // 理财直融新增
    *finaFlockingSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.finaFlockingSave, payload);
      callback(response);
    },
    // 存款新增
    *addNewSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.addNewSave, payload);
      if (response && response.success) {
        callback('success', response);
      } else {
        callback('false', response);
      }
    },
    // etf新增
    *etfSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.etfSave, payload);
      if (response && response.success) {
        callback(true);
        message.success('组合试算新增成功');
      } else {
        callback();
        message.success(response.errorMsg || '组合试算新增失败');
      }
    },
    // 非标新增
    *nonstandardOrderSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.nonstandardOrderSave, payload);
      callback(response);
    },
    // 股票新增
    *stockSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.stockSave, payload);
      if (response && response.success) {
        message.success('组合试算新增成功!');
      } else {
        message.error(response.errorMsg || '组合试算新增失败!');
      }
      return response;
    },
    //资产管理新增
    *zcglPlanSave({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioTrial.zcglPlanSave, payload);
      if (response && response.success) {
        message.success('组合试算新增成功!');
        callback(true);
      } else {
        message.error(response.errorMsg || '组合试算新增失败!');
      }
      return response;
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
